﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Yxinmt.Controls
{
    public class BaseControl : IControl
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string FullName { get; set; }
        public Dictionary<string, object> Property { get; set; }
        public System.Windows.FrameworkElement Element { get; set; }
        protected System.Windows.Controls.Control _Control { get; set; }
        public System.Windows.Controls.DataGridColumn Column { get; set; }
        public System.Windows.Controls.GridViewColumn Column2 { get; set; }

        protected IComponent _Component { get; set; }
        protected IWindow _Window { get; set; }
        protected bool _IsFactory { get; set; }
        protected List<FactoryProperty> _FactoryPropertyList { get; set; }
        protected Type _ControlType { get; set; }
        protected double _Opacity { get; set; }
        protected bool _IsAddToControls { get; set; }

        protected void InitSet()
        {
            Id = Property.ContainsKey("id") ? Property.GetValue<Guid>("id") : Guid.NewGuid();
            Name = Property.GetStringValue("name");
            FullName = string.Format("{0}_{1}", this.GetType().Name, Name);

            _Opacity = Property.GetValue<double>("Opacity");
            _IsAddToControls = Property.ContainsKey("IsAddToControls") ? Property.GetValue<bool>("IsAddToControls") : true;

            if (Element is System.Windows.Controls.Control) _Control = (System.Windows.Controls.Control)Element;

            _Window = _Component.Window;
            if (_IsAddToControls && !_IsFactory) this._Window.ControlList.Add(this);

            SetDataGridViewControls();
        }

        public IComponent GetComponent() { return _Component; }

        private void SetDataGridViewControls()
        {
            if (_Window.DataGridViewControlsDictionary != null && Property.ContainsKey("DataGridViewId"))
            {
                Guid dataGridViewId = Property.GetValue<Guid>("DataGridViewId");
                if (_Window.DataGridViewControlsDictionary.ContainsKey(dataGridViewId))
                {
                    _DataGridViewControls = _Window.DataGridViewControlsDictionary[dataGridViewId];
                    if (Property.ContainsKey("IsColumn")) _DataGridViewControls.ColumnControlList.Add(this);
                    else if (Property.ContainsKey("IsRow")) _DataGridViewControls.RowControlList.Add(this);
                }
            }
        }

        protected IDataGridViewControls _DataGridViewControls { get; set; }

        public virtual void Clear()
        {
        }

        protected List<System.Windows.Input.Key> InitNumberKeyList()
        {
            return new List<System.Windows.Input.Key>()
            {
                System.Windows.Input.Key.D0,
                System.Windows.Input.Key.D1,
                System.Windows.Input.Key.D2,
                System.Windows.Input.Key.D3,
                System.Windows.Input.Key.D4,
                System.Windows.Input.Key.D5,
                System.Windows.Input.Key.D6,
                System.Windows.Input.Key.D7,
                System.Windows.Input.Key.D8,
                System.Windows.Input.Key.D9,
                System.Windows.Input.Key.NumPad0,
                System.Windows.Input.Key.NumPad1,
                System.Windows.Input.Key.NumPad2,
                System.Windows.Input.Key.NumPad3,
                System.Windows.Input.Key.NumPad4,
                System.Windows.Input.Key.NumPad5,
                System.Windows.Input.Key.NumPad6,
                System.Windows.Input.Key.NumPad7,
                System.Windows.Input.Key.NumPad8,
                System.Windows.Input.Key.NumPad9
            };
        }

        /// <summary>
        /// 事件加载
        /// </summary>
        public virtual void EventLoad()
        {
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        public virtual void DataLoad()
        {
        }

        public System.Windows.FrameworkElementFactory GetFactory()
        {
            var factory = new System.Windows.FrameworkElementFactory(_ControlType);
            factory.Name = FullName;

            _FactoryPropertyList.ForEach(p =>
            {
                factory.SetValue(p.Property, p.Value);
            });

            return factory;

        }

        protected System.Windows.Input.Cursor GetCursor(string value)
        {
            switch (value)
            {
                case "Wait": return System.Windows.Input.Cursors.Wait;
                case "Cross": return System.Windows.Input.Cursors.Cross;
                case "Hand": return System.Windows.Input.Cursors.Hand;
                case "IBeam": return System.Windows.Input.Cursors.IBeam;
                case "Pen": return System.Windows.Input.Cursors.Pen;
                default: return System.Windows.Input.Cursors.Arrow;
            }
        }

        protected void SetCanvasLocation(string value)
        {
            string[] vs = value.Split(',');
            if (vs.Length == 4)
            {
                double left = double.Parse(vs[0]);
                double top = double.Parse(vs[1]);
                double right = double.Parse(vs[2]);
                double bottom = double.Parse(vs[3]);

                if (left > 0) System.Windows.Controls.Canvas.SetLeft(Element, left);
                if (top > 0) System.Windows.Controls.Canvas.SetTop(Element, top);
                if (right > 0) System.Windows.Controls.Canvas.SetRight(Element, right);
                if (bottom > 0) System.Windows.Controls.Canvas.SetBottom(Element, bottom);
            }
        }

        protected void SetCanvasZIndex(int value)
        {
            System.Windows.Controls.Canvas.SetZIndex(Element, value);
        }

        /// <summary>
        /// 设置位置
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void SetLocation(int x, int y)
        {
            Element.Margin = new System.Windows.Thickness(x, y, 0, 0);
            this.SetFactoryProperty("Margin", Element.Margin);
        }

        protected void SetPropertyKvp(List<string> keyList, Dictionary<string, object> property)
        {
            keyList.ForEach(key => { if (Property.ContainsKey(key) && !property.ContainsKey(key)) property[key] = Property[key]; });
        }

        protected void AddFactoryProperty(System.Windows.DependencyProperty property, object value)
        {
            this._FactoryPropertyList.Add(new FactoryProperty(property, value));
        }

        protected void SetDefaultValue(string name, object value)
        {
            if (!this.Property.ContainsKey(name)) this.Property[name] = value;
        }

        protected void SetLabelLocation(System.Drawing.Point startPoint)
        {
            int x = Property.GetValue<int>("X");
            int y = Property.GetValue<int>("Y");
            int width = Property.GetValue<int>("Width");
            int height = Property.GetValue<int>("Height");
            int marginTop = Property.GetValue<int>("LabelMarginTop");

            int x2 = (y - 1) * width + startPoint.X;
            int y2 = (x - 1) * height + marginTop + startPoint.Y;

            this.SetLocation(x2, y2);
        }

        protected void SetControlLocation(System.Drawing.Point startPoint)
        {
            int x = Property.GetValue<int>("X");
            int y = Property.GetValue<int>("Y");
            int width = Property.GetValue<int>("Width");
            int height = Property.GetValue<int>("Height");
            int marginTop = Property.GetValue<int>("ControlMarginTop");
            int marginLeft = Property.GetValue<int>("ControlMarginLeft");
            int labelWidth = Property.GetValue<int>("LabelWidth");

            int x2 = (y - 1) * width + marginLeft + labelWidth + startPoint.X;
            int y2 = (x - 1) * height + marginTop + startPoint.Y;

            this.SetLocation(x2, y2);
        }

        protected void SetControlProperty(bool blLabel = false, bool blControl = true)
        {
            bool isPoint = Property.ContainsKey("IsPoint") ? Property.GetValue<bool>("IsPoint") : true;
            if (isPoint)
            {
                int x = Property.GetValue<int>("X");
                int y = Property.GetValue<int>("Y");
                this.SetLocation(x, y);
            }
            else if (blLabel) this.SetLabelLocation(_Window.StartPoint);
            else if (blControl) this.SetControlLocation(_Window.StartPoint);

            if (_Control != null) _Control.TabIndex = _Window.TabIndex;
            _Window.TabIndex += 1;

            this.SetFont();

            foreach (var kvp in Property)
            {
                this.SetProperty(kvp.Key, kvp.Value);
            }
        }

        protected void SetColumnProperty()
        {
            foreach (var kvp in Property)
            {
                this.SetProperty(kvp.Key, kvp.Value);
            }
        }

        protected System.Windows.Media.ImageSource GetImage(string url)
        {
            return Utility.Common.GetImageSource(url);
        }

        protected System.Windows.Media.Stretch GetStretch(string value)
        {
            switch (value)
            {
                case "Fill": return System.Windows.Media.Stretch.Fill;
                case "Uniform": return System.Windows.Media.Stretch.Uniform;
                case "UniformToFill": return System.Windows.Media.Stretch.UniformToFill;
                default: return System.Windows.Media.Stretch.None;
            }
        }

        protected void SetFont()
        {
            if (_Control == null) return;

            string fontFamily = this.Property.GetStringValue("FontFamily");
            double fontSize = this.Property.GetValue<double>("FontSize");
            string fontColor = this.Property.GetStringValue("FontColor");
            bool isBold = this.Property.GetValue<bool>("IsBold");

            if (!string.IsNullOrEmpty(fontFamily))
            {
                this._Control.FontFamily = new System.Windows.Media.FontFamily(fontFamily);
                this.SetFactoryProperty("FontFamily", this._Control.FontFamily);
            }
            if (fontSize > 0)
            {
                this._Control.FontSize = fontSize;
                this.SetFactoryProperty("FontSize", this._Control.FontSize);
            }
            if (!string.IsNullOrEmpty(fontColor))
            {
                this._Control.Foreground = new System.Windows.Media.SolidColorBrush(Utility.Common.GetColor(fontColor));
                this.SetFactoryProperty("Foreground", this._Control.Foreground);
            }
            if (isBold)
            {
                this._Control.FontWeight = System.Windows.FontWeights.Bold;
                this.SetFactoryProperty("FontWeight", this._Control.FontWeight);
            }
        }

        protected virtual void SetProperty(string name, object value)
        {

        }

        protected virtual void SetFactoryProperty(string name, object value)
        {

        }

        public void SetFontSizeColor(string fontFamily, double fontSize, string fontColor)
        {
            this.Property["FontFamily"] = fontFamily;
            this.Property["FontSize"] = fontSize;
            this.Property["FontColor"] = fontColor;
            this.SetFont();
        }

        public virtual object GetValue()
        {
            return string.Empty;
        }

        public virtual void SetValue(object value)
        {

        }

        public virtual bool ValidateNullable(out string message)
        {
            message = string.Empty;
            return true;
        }

        protected System.Windows.Controls.Dock GetDock(string value)
        {
            switch (value)
            {
                case "Bottom": return System.Windows.Controls.Dock.Bottom;
                case "Right": return System.Windows.Controls.Dock.Right;
                case "Left": return System.Windows.Controls.Dock.Left;
                default: return System.Windows.Controls.Dock.Top;
            }
        }

        protected System.Windows.Thickness GetThickness(string value)
        {
            string[] vs = value.Split(',');
            if (vs.Length == 4)
            {
                return new System.Windows.Thickness(double.Parse(vs[0]), double.Parse(vs[1]), double.Parse(vs[2]), double.Parse(vs[3]));
            }

            return new System.Windows.Thickness();
        }

        protected System.Windows.TextWrapping GetTextWrapping(string value)
        {
            switch (value)
            {
                case "NoWrap": return System.Windows.TextWrapping.NoWrap;
                case "WrapWithOverflow": return System.Windows.TextWrapping.WrapWithOverflow;
                default: return System.Windows.TextWrapping.Wrap;
            }
        }

        protected void SetGridRowColumn(string value)
        {
            string[] vs = value.Split(new char[] { ',', '，' });
            System.Windows.Controls.Grid.SetRow(this.Element, int.Parse(vs[0]));
            System.Windows.Controls.Grid.SetColumn(this.Element, int.Parse(vs[1]));
        }

        protected System.Windows.HorizontalAlignment GetVHorizontalAlignment(string value)
        {
            switch (value)
            {
                case "BottomRight":
                case "TopRight":
                case "MiddleRight": return System.Windows.HorizontalAlignment.Right;
                case "BottomCenter":
                case "TopCenter":
                case "MiddleCenter": return System.Windows.HorizontalAlignment.Center;
                case "BottomLeft":
                case "TopLeft":
                case "MiddleLeft": return System.Windows.HorizontalAlignment.Left;
                default: return System.Windows.HorizontalAlignment.Left;
            }
        }

        protected System.Windows.VerticalAlignment GetHVerticalAlignment(string value)
        {
            switch (value)
            {
                case "BottomRight":
                case "BottomLeft":
                case "BottomCenter": return System.Windows.VerticalAlignment.Bottom;
                case "TopRight":
                case "TopCenter":
                case "TopLeft": return System.Windows.VerticalAlignment.Top;
                case "MiddleRight":
                case "MiddleCenter":
                case "MiddleLeft": return System.Windows.VerticalAlignment.Center;
                default: return System.Windows.VerticalAlignment.Top;
            }
        }

        protected void SetControlAlign(string value)
        {
            this.Element.VerticalAlignment = this.GetHVerticalAlignment(value);
            this.Element.HorizontalAlignment = this.GetVHorizontalAlignment(value);

            this.SetFactoryProperty("VerticalAlignment", this.Element.VerticalAlignment);
            this.SetFactoryProperty("HorizontalAlignment", this.Element.HorizontalAlignment);
        }

        protected void SetTextAlign(string value)
        {
            if (_Control == null) return;
            this._Control.VerticalContentAlignment = this.GetHVerticalAlignment(value);
            this._Control.HorizontalContentAlignment = this.GetVHorizontalAlignment(value);

            this.SetFactoryProperty("VerticalContentAlignment", this._Control.VerticalContentAlignment);
            this.SetFactoryProperty("HorizontalContentAlignment", this._Control.HorizontalContentAlignment);
        }

        public virtual void SetVisible(bool visible)
        {
            if (visible && this.Element.Visibility == System.Windows.Visibility.Visible) return;
            else if (!visible && this.Element.Visibility == System.Windows.Visibility.Collapsed) return;

            if (this.Element != null) this.Element.Visibility = visible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;

            this.SetFactoryProperty("Visibility", this.Element.Visibility);
        }

        protected System.Windows.Visibility GetVisible(bool visible)
        {
            return visible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }


        protected System.Windows.FlowDirection GetFlowDirection(string value)
        {
            switch (value)
            {
                case "Right": return System.Windows.FlowDirection.RightToLeft;
                default: return System.Windows.FlowDirection.LeftToRight;
            }
        }

        protected System.Windows.VerticalAlignment GetVerticalAlignment(string value)
        {
            switch (value)
            {
                case "Bottom": return System.Windows.VerticalAlignment.Bottom;
                case "Center": return System.Windows.VerticalAlignment.Center;
                case "Stretch": return System.Windows.VerticalAlignment.Stretch;
                default: return System.Windows.VerticalAlignment.Top;
            }
        }

        protected System.Windows.HorizontalAlignment GetHorizontalAlignment(string value)
        {
            switch (value)
            {
                case "Center": return System.Windows.HorizontalAlignment.Center;
                case "Right": return System.Windows.HorizontalAlignment.Right;
                case "Stretch": return System.Windows.HorizontalAlignment.Stretch;
                default: return System.Windows.HorizontalAlignment.Left;
            }
        }

        protected void SetPropertyKvp(List<string> keyList, Dictionary<string, object> property, List<string> noContainNameList)
        {
            keyList.ForEach(key => { if (Property.ContainsKey(key) && !property.ContainsKey(key) && !noContainNameList.Contains(key)) property[key] = Property[key]; });
        }

        protected System.Windows.Controls.Orientation GetOrientation(string value)
        {
            switch (value)
            {
                case "Vertical": return System.Windows.Controls.Orientation.Vertical;
                default: return System.Windows.Controls.Orientation.Horizontal;
            }
        }

        protected void SetZIndex(int value)
        {
            System.Windows.Controls.Panel.SetZIndex(this.Element, value);
        }

        System.Windows.Threading.DispatcherTimer _DispatcherTimer { get; set; }

        public void BindClick(bool isClick = true, bool isDoubleClick = false)
        {
            this.Element.MouseLeftButtonDown += Control_MouseDown;
            this.Element.MouseUp += Control_MouseUp;

            MouseClickAction2(isClick, isDoubleClick);
        }

        void MouseClickAction2(bool isClick = true, bool isDoubleClick = false)
        {
            if (isDoubleClick && isClick) MounseClickAction = (sender, e, ctl) => SingleDoubleClick(sender, e);
            else if (isDoubleClick) MounseClickAction = (sender, e, ctl) => { if (_ClickCount > 1) { _ClickCount = 0; if (DoubleClickAction != null) DoubleClickAction(sender, e, this); } };
            else if (isClick) MounseClickAction = (sender, e, ctl) => { if (ClickAction != null) ClickAction(sender, e, ctl); };
        }

        public void BindPreviewClick(bool isClick = true, bool isDoubleClick = false)
        {
            this.Element.PreviewMouseLeftButtonDown += Control_MouseDown;
            this.Element.PreviewMouseUp += Control_MouseUp;

            MouseClickAction2(isClick, isDoubleClick);
        }

        public void BindMouseOver()
        {
            this.Element.MouseEnter += Control_MouseEnter;
            this.Element.MouseLeave += Control_MouseLeave;
        }

        public Action<object, System.Windows.RoutedEventArgs> MouseEnter { get; set; }
        public Action<object, System.Windows.RoutedEventArgs> MouseLeave { get; set; }

        void Control_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (MouseLeave != null) MouseLeave(sender, e);
        }

        void Control_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (MouseEnter != null) MouseEnter(sender, e);
        }

        private void SingleDoubleClick(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (_ClickCount == 1)
                {
                    _DispatcherTimer = new System.Windows.Threading.DispatcherTimer();
                    _DispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 300);
                    _DispatcherTimer.Tick += (s, e1) => { _ClickCount = 0; _DispatcherTimer.IsEnabled = false; if (ClickAction != null) ClickAction(sender, e, this); };
                    _DispatcherTimer.IsEnabled = true;
                }
                else if (_DispatcherTimer != null)
                {
                    _DispatcherTimer.IsEnabled = false;
                    _ClickCount = 0;
                    if (DoubleClickAction != null) DoubleClickAction(sender, e, this);
                    _DispatcherTimer = null;
                }
            }
            catch (Exception ex)
            {
                Utility.LoggerProxy.Exception(Name, "SingleDoubleClick", ex);
            }
        }

        protected bool _IsMouseDown { get; set; }
        public Action<object, System.Windows.RoutedEventArgs, IControl> ClickAction { get; set; }
        public Action<object, System.Windows.RoutedEventArgs, IControl> DoubleClickAction { get; set; }
        Action<object, System.Windows.RoutedEventArgs, IControl> MounseClickAction { get; set; }
        protected int _ClickCount { get; set; }

        public Action<object, System.Windows.RoutedEventArgs, IControl> MouseDownAction { get; set; }
        public Action<object, System.Windows.RoutedEventArgs, IControl> MouseUpAction { get; set; }

        void Control_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            MouseUp(sender, e);
        }

        void MouseUp(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                if (_IsMouseDown)
                {
                    _IsMouseDown = false;
                    MounseClickAction(sender, e, this);
                    _ClickCount = 0;
                }

                if (MouseUpAction != null) MouseUpAction(sender, e, this);
            }
            catch (Exception ex)
            {
                LoggerProxy.Exception(Name, "MouseUp", ex);
            }
        }

        void Control_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            MouseDown(sender, e);
        }

        void MouseDown(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                _IsMouseDown = true;
                if (e is System.Windows.Input.MouseButtonEventArgs) _ClickCount = ((System.Windows.Input.MouseButtonEventArgs)e).ClickCount;
                else _ClickCount += 1;

                if (MouseDownAction != null) MouseDownAction(sender, e, this);
            }
            catch (Exception ex)
            {
                LoggerProxy.Exception(Name, "MouseDown", ex);
            }
        }

        public virtual void Add(System.Windows.FrameworkElement control)
        {
        }

        public virtual void Remove(System.Windows.FrameworkElement element)
        {

        }

        protected System.Windows.Media.SolidColorBrush GetColor(string value)
        {
            var color = new System.Windows.Media.SolidColorBrush(Common.GetColor((string)value));
            if (_Opacity > 0) color.Opacity = _Opacity / 100;
            return color;
        }

        public void SetBackgroundImage(string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                string stretch = Property.GetStringValue("BackImageStretch");
                stretch = string.IsNullOrEmpty(stretch) ? "UniformToFill" : stretch;
                System.Windows.Media.ImageBrush img = new System.Windows.Media.ImageBrush();
                img.ImageSource = GetImage(url);
                img.Stretch = this.GetStretch(stretch);

                if (_Control != null) SetControlBackground(img);
                else if (Element is System.Windows.Controls.Panel) SetPanelBackground(img);
            }
            else
            {
                if (_Control != null) SetControlBackground(GetColor("Transparent"));
                else if (Element is System.Windows.Controls.Panel) SetPanelBackground(GetColor("Transparent"));
            }
        }

        public System.Windows.Media.ImageBrush GetImageBrush(string url)
        {
            string stretch = Property.GetStringValue("BackImageStretch");
            stretch = string.IsNullOrEmpty(stretch) ? "UniformToFill" : stretch;
            System.Windows.Media.ImageBrush img = new System.Windows.Media.ImageBrush();
            img.ImageSource = GetImage(url);
            img.Stretch = this.GetStretch(stretch);

            return img;
        }

        protected string GetImagePath(string url)
        {
            return string.Concat(AppDomain.CurrentDomain.BaseDirectory, url.Replace("/", "\\"));
        }

        public void SetWidthHeight(double width, double height)
        {
            Element.Width = width;
            Element.Height = height;
        }

        public void SetWidth(double width)
        {
            Element.Width = width;
        }

        public void SetHeight(double height)
        {
            Element.Height = height;
        }

        public void SetMargin(string margin)
        {
            Element.Margin = this.GetThickness(margin);
        }

        public void SetBackgroundImage(System.Windows.Media.ImageSource imageSource)
        {
            if (imageSource != null)
            {
                string stretch = Property.GetStringValue("BackImageStretch");
                stretch = string.IsNullOrEmpty(stretch) ? "UniformToFill" : stretch;
                System.Windows.Media.ImageBrush img = new System.Windows.Media.ImageBrush();
                img.ImageSource = imageSource;
                img.Stretch = this.GetStretch(stretch);

                if (_Control != null) SetControlBackground(img);
                else if (Element is System.Windows.Controls.Panel) SetPanelBackground(img);
            }
            else
            {
                if (_Control != null) SetControlBackground(GetColor("Transparent"));
                else if (Element is System.Windows.Controls.Panel) SetPanelBackground(GetColor("Transparent"));
            }
        }

        public System.Windows.Media.ImageBrush GetBackgroundImage(string url, string stretch)
        {
            stretch = string.IsNullOrEmpty(stretch) ? "UniformToFill" : stretch;

            System.Windows.Media.ImageBrush img = new System.Windows.Media.ImageBrush();
            img.ImageSource = GetImage(url);
            img.Stretch = this.GetStretch(stretch);
            return img;
        }

        private void SetPanelBackground(System.Windows.Media.Brush brush)
        {
            (Element as System.Windows.Controls.Panel).Background = brush;
            SetFactoryProperty("Background", (Element as System.Windows.Controls.Panel).Background);
        }

        private void SetBorderBackground(System.Windows.Media.Brush brush)
        {
            (Element as System.Windows.Controls.Border).Background = brush;
            SetFactoryProperty("Background", (Element as System.Windows.Controls.Border).Background);
        }

        private void SetControlBackground(System.Windows.Media.Brush brush)
        {
            this._Control.Background = brush;
            SetFactoryProperty("Background", this._Control.Background);
        }

        public bool IsVisible
        {
            get { return Element.Visibility == System.Windows.Visibility.Visible; }
        }

        protected System.Windows.Controls.ScrollBarVisibility GetScrollBarVisibility(string value)
        {
            switch (value)
            {
                case "Disabled": return System.Windows.Controls.ScrollBarVisibility.Disabled;
                case "Hidden": return System.Windows.Controls.ScrollBarVisibility.Hidden;
                case "Visible": return System.Windows.Controls.ScrollBarVisibility.Visible;
                default: return System.Windows.Controls.ScrollBarVisibility.Auto;
            }
        }

        public virtual void SetBackColor(string color)
        {
            color = string.IsNullOrEmpty(color) ? "Transparent" : color;

            if (_Control != null) SetControlBackground(GetColor(color));
            else if (Element is System.Windows.Controls.Panel) SetPanelBackground(GetColor(color));
            else if (Element is System.Windows.Controls.Border) SetBorderBackground(GetColor(color));
        }

        protected string GetValueText(object value)
        {
            if (value == null) return string.Empty;

            string dataType = Property.GetStringValue("DataType");
            if (string.IsNullOrEmpty(dataType)) return value.ToString();
            else
            {
                if (dataType == "DateTime")
                {
                    DateTime date = (DateTime)Utility.Common.ChangeType(value, typeof(DateTime));
                    return date == DateTime.MinValue ? string.Empty : date.ToString("yyyy-MM-dd HH:mm:sss");
                }
                else return value.ToString();
            }
        }

        protected Entity.Point GetMousePoint(System.Windows.Input.InputEventArgs e, System.Windows.IInputElement relativeTo)
        {
            return new Entity.Point(((System.Windows.Input.MouseEventArgs)e).GetPosition(relativeTo));
        }

        protected void AddControl<T>(List<T> list) where T : IControl
        {
            T ctl = list.Where(w => w.Name == Name).FirstOrDefault();
            if (ctl != null) list.Remove(ctl);

            list.Add((T)((IControl)this));
        }

        public byte[] GetControlBitmap()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                System.Windows.Media.Imaging.BitmapEncoder encoder = new System.Windows.Media.Imaging.PngBitmapEncoder();
                System.Windows.Media.Imaging.RenderTargetBitmap bitmap = new System.Windows.Media.Imaging.RenderTargetBitmap(Convert.ToInt32(Element.ActualWidth), Convert.ToInt32(Element.ActualHeight), 0, 0, System.Windows.Media.PixelFormats.Default);
                bitmap.Render(Element);

                encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(bitmap));
                encoder.Save(ms);

                return ms.ToArray();
            }
        }

        public int GetCount()
        {
            if (Element is System.Windows.Controls.Panel p) return p.Children.Count;
            return 0;
        }

        public T GetItem<T>(int index) where T : System.Windows.FrameworkElement
        {
            if (Element is System.Windows.Controls.Panel p && p.Children[index] is T t) return t;
            return default(T);
        }

        public virtual T GetChild<T>() where T : System.Windows.FrameworkElement
        {
            return default(T);
        }
    }
}
